package com.faxsun.core.offer.service;

import java.util.ArrayList;
import java.util.List;

import org.broadleafcommerce.core.offer.domain.Offer;
import org.broadleafcommerce.core.offer.service.OfferContext;
import org.broadleafcommerce.core.offer.service.OfferServiceImpl;
import org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateFulfillmentGroupOffer;
import org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateItemOffer;
import org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateOrderOffer;
import org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder;
import org.broadleafcommerce.core.offer.service.type.OfferType;
import org.broadleafcommerce.core.order.domain.Order;

public class FSOfferServiceImpl extends OfferServiceImpl implements FSOfferService {

	public boolean couldOffersApplyToOrder(final List<Offer> offers, final Order order) {

		OfferContext offerContext = OfferContext.getOfferContext();
		if (offerContext == null || offerContext.getExecutePromotionCalculation()) {
			PromotableOrder promotableOrder = promotableItemFactory.createPromotableOrder(order, false);
			List<Offer> filteredOffers = orderOfferProcessor.filterOffers(offers, order.getCustomer());
			/**
			 * Offer validate steps:
			 * 1. if filtered offers empty, return false;
			 * 2.1 if either qualifiedItemOffers or qualifiedOrderOffers not empty, return true;
			 * 2.2 else, if qualifiedFGOffers is not empty, return true;
			 * 3. else, return false
			 */

			if ((filteredOffers == null) || filteredOffers.isEmpty()) {
				// filtered offers empty, return false;
				return false;
			}

			List<PromotableCandidateOrderOffer> qualifiedOrderOffers = new ArrayList<PromotableCandidateOrderOffer>();
			List<PromotableCandidateItemOffer> qualifiedItemOffers = new ArrayList<PromotableCandidateItemOffer>();

			itemOfferProcessor.filterOffers(promotableOrder, filteredOffers, qualifiedOrderOffers,
					qualifiedItemOffers);

			if (!qualifiedItemOffers.isEmpty() || !qualifiedOrderOffers.isEmpty()) {
				// either qualifiedItemOffers or qualifiedOrderOffers not empty, return true;
				return true;
				
			} else {
				List<Offer> possibleFGOffers = new ArrayList<Offer>();
				for (Offer offer : offers) {
					if (offer.getType().getType().equals(OfferType.FULFILLMENT_GROUP.getType())) {
						possibleFGOffers.add(offer);
					}
				}
				List<Offer> filteredFGOffers = orderOfferProcessor.filterOffers(possibleFGOffers,
						order.getCustomer());
				List<PromotableCandidateFulfillmentGroupOffer> qualifiedFGOffers = new ArrayList<PromotableCandidateFulfillmentGroupOffer>();
				for (Offer offer : filteredFGOffers) {
					fulfillmentGroupOfferProcessor.filterFulfillmentGroupLevelOffer(promotableOrder,
							qualifiedFGOffers, offer);
				}
				if (!qualifiedFGOffers.isEmpty()) {
					// if qualifiedFGOffers is not empty, return true;
					return true;
				}
			}
		}
		// No offer applicable, return false.
		return false;
	}
}
